
export const fetchPrefix = 'http://localhost:5000';

export const tokenData = {};

const needFilterMethods = ['POST', 'DELETE']

// 目前仅简单处理post请求
const filterOptions = options => {
    if (needFilterMethods.includes(options.method?.toUpperCase())) {
        options = {
            ...options,
            headers: {
                ...(options.headers || {}),
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(options.body)
        }
    }
    options.headers = options.headers || {};
    options.headers.Authorization = tokenData?.Authorization;
    return options
}

// 简单的fetch封装
export default (url, options = {}) => {
    return new Promise(async (resolve, reject) => {
        options = filterOptions(options);
        fetch(fetchPrefix + url, options)
            .then(res => resolve(res.json()))
            .catch(error => reject(error));
    })
};